package com.export.xlsx.utils;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ==================================================================
 * <p>
 * Description: 日期工具类
 * <p>
 * Author: ryu
 * <p>
 * Date: 2018/1/23
 * <p>
 * Version: v1.0
 * <p>
 * Copyright: All rights reserved by Ryu.
 * ==================================================================
 */
public class DateUtils {

    /** 时间格式(yyyy-MM-dd) */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /** 时间格式(yyyy-MM-dd HH:mm) */
    public final static String DATE_PATTERN_TO_MINUTE = "yyyy-MM-dd HH:mm";
    /** 时间格式(yyyy-MM-dd HH:mm:ss) */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";



    /**
     * 将字符串转化成Date
     * @param dateStr
     * @return
     */
    public static Date parseToDate(String dateStr, String pattern){
        Date date = null;

        if(StringUtils.isEmpty(dateStr)){
            return date;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    /**
     * 将日期转换成字符串
     * @param date
     * @param pattern
     * @return
     */
    public static String formatDateToStr(Date date, String pattern){
        String dateStr;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);

        dateStr = sdf.format(date);

        return dateStr;
    }

}
